import Vue from 'vue'
import VueRouter from 'vue-router'
import $store from '@/store'
import $http from '@/utils/request/request.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: () => import('@/views/ecg/Index'),
    meta: {
        title: '心电信息'
    }
  },
  {
    path: '/userinfo',
    name: 'userInfoDetail',
    component: () => import('@/views/user/userInfoDetail'),
    meta: {
        title: '用户信息详情'
    }
  },
  {
    path: '/addremind',
    name: 'addRemind',
    component: () => import('@/views/add/addRemind'),
    meta: {
        title: '添加强提醒'
    }
  },
  {
    path: '/addcaretxt',
    name: 'addCareTxt',
    component: () => import('@/views/add/addCareTxt'),
    meta: {
        title: '添加护理事项'
    }
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
    window.document.title = to.meta.title || ''
    const ecgConfig = $store.state.ecgConfig
    if (ecgConfig) {
        next()
    } else {
        $http.post('/zyb/web/v3/getEcgConfig').then(res => {
            const data = res.data
            const config = {
                temperature: {
                    max: data.high_temperature,
                    min: data.low_temperature
                },

                systolicPressure: {
                    max: data.high_systolic_pressure,
                    min: data.low_systolic_pressure
                },
                diastolicPressure: {
                    max: data.high_diastolic_pressure,
                    min: data.low_diastolic_pressure
                },
                bloodoxygen: {
                    max: data.high_spo2,
                    min: data.low_spo2
                },
                heartbeat: {
                    max: data.high_ecg,
                    min: data.low_ecg
                }
            }
            $store.commit('SAVE_ECG_CONGIG', config)
            next()
        }).catch(()=>{
            next()
        })
    }
})

export default router
